/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listener;

import com.google.gson.Gson;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import listener.MyEvent;
import protocol.Command;
import server.ServeOneThread;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

/**
 *
 * @author User
 */
public class Proxy implements MyListener {
    MyListener n; //= ForProxyFactorry.getInstance("hiber"); //hiber "hand"
    
    public Proxy(){
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src\\SpringXMLConfig.xml"));
        ProxySeter proxySeter = (ProxySeter) factory.getBean("proxySeter");
        n = proxySeter.getServerListener();
    }
    

  @Override
  public boolean autorize(MyEvent e) {
    n.autorize(e);
    return true;
  }

  @Override
  public String setList(MyEvent e) {
    String str = n.setList(e);
    return str;
  }

  @Override
  public String getPersone(MyEvent evn) {
    String str = n.getPersone(evn);
    return str;
  }

  @Override
  public boolean sendPhoneT(MyEvent evn) {
    n.sendPhoneT(evn);
    return true;
  }

  @Override
  public boolean sendAdrT(MyEvent evn) {
    n.sendAdrT(evn);
    return true;
  }

  @Override
  public boolean sendCountry(MyEvent evn) {
    n.sendCountry(evn);
    return true;
  }

  @Override
  public boolean sendCity(MyEvent evn) {
    n.sendCity(evn);
    return true;
  }

  @Override
  public boolean sendStreet(MyEvent evn) {
    n.sendStreet(evn);
    return true;
  }

  @Override
  public boolean savePersone(MyEvent evn) {
    if (evn.fp.uid != null) {
      if (evn.fp.uid.equals(evn.mysrv.p.getId()) || evn.mysrv.p.getRole().equals("Admin")) {
        n.savePersone(evn);
      } else {
        try {
          sendError(evn.mysrv);
        } catch (IOException ex) {
          Logger.getLogger(Proxy.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    } else {
      n.savePersone(evn);
    }
    
    return true;
  }

  @Override
  public boolean saveTypePhone(MyEvent evn) {
    if (evn.mysrv.p.getRole().equals("User")){
      try {
        sendError(evn.mysrv);
      } catch (IOException ex) {
        Logger.getLogger(Proxy.class.getName()).log(Level.SEVERE, null, ex);
      }
    } else {
      n.saveTypePhone(evn);
    }
    return true;
  }

  @Override
  public boolean saveTypeAdress(MyEvent evn) {
    if (evn.mysrv.p.getRole().equals("User")){
      try {
        sendError(evn.mysrv);
      } catch (IOException ex) {
        Logger.getLogger(Proxy.class.getName()).log(Level.SEVERE, null, ex);
      }
    } else {
      n.saveTypeAdress(evn);
    }
    return true;
  }

  @Override
  public boolean saveCountry(MyEvent evn) {
    if (evn.mysrv.p.getRole().equals("User") || evn.mysrv.p.getRole().equals("SUser")){
      try {
        sendError(evn.mysrv);
      } catch (IOException ex) {
        Logger.getLogger(Proxy.class.getName()).log(Level.SEVERE, null, ex);
      }
    } else {
      n.saveCountry(evn);
    }
    return true;
  }

  @Override
  public boolean saveCity(MyEvent evn) {
    if (evn.mysrv.p.getRole().equals("User") || evn.mysrv.p.getRole().equals("SUser")){
      try {
        sendError(evn.mysrv);
      } catch (IOException ex) {
        Logger.getLogger(Proxy.class.getName()).log(Level.SEVERE, null, ex);
      }
    } else {
      n.saveCity(evn);
    }
    return true;
  }

  @Override
  public boolean saveStreet(MyEvent evn) {
    if (evn.mysrv.p.getRole().equals("User") || evn.mysrv.p.getRole().equals("SUser")){
      try {
        sendError(evn.mysrv);
      } catch (IOException ex) {
        Logger.getLogger(Proxy.class.getName()).log(Level.SEVERE, null, ex);
      }
    } else {
      n.saveStreet(evn);
    }
    return true;
  }

  @Override
  public boolean delPersone(MyEvent evn) {
    n.delPersone(evn);
    return true;
  }

  private void sendError(ServeOneThread mysrv) throws IOException {
    Command obj = new Command();
    obj.Type = "Connect";
    obj.Data = "Error";
    obj.Add = "access denied";
    Gson gson = new Gson();
    String send = gson.toJson(obj);
    mysrv.sendUser(send);
  }

    @Override
    public boolean delPhone(MyEvent myEvent) {
        n.delPhone(myEvent);
        return true;
    }

    @Override
    public boolean delAdres(MyEvent myEvent) {
        n.delAdres(myEvent);
        return true;
    }

  @Override
  public boolean getFoto(MyEvent myEvent) {
    n.getFoto(myEvent);
    return true;
  }
  
}
